package net.zhengxinyun.performance.App.service.leaderCriteria;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.zhengxinyun.performance.App.dto.ScoreDTO;
import net.zhengxinyun.performance.App.entity.EvaluateIndexEnrity;
import net.zhengxinyun.performance.App.entity.EvaluateScoreEntity;
import net.zhengxinyun.performance.App.mapper.EvaScoreMageMapper;
import net.zhengxinyun.performance.Util.DataUtil;
import net.zhengxinyun.performance.entity.CriteriaEntity;
import net.zhengxinyun.performance.filter.CriteriaFilter;
import net.zhengxinyun.performance.filter.EvaluateIndexFilter;
import net.zhengxinyun.performance.filter.EvaluateScoreFilter;
import net.zhengxinyun.performance.service.criteria.CriteriaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * Date: 2020/3/17 16:15
 */
@Service
public class LeaderCriteriaServiceImpl implements LeaderCriteriaService {

    @Autowired
    private EvaScoreMageMapper evaScoreMageMapper;
    @Autowired
    private CriteriaService criteriaService;


    /**单个考核子项的得分
     *
     * @return
     */
    @Override
    public EvaluateScoreEntity detailItemScore(Long evalId, int calculates,String grade) {
        EvaluateScoreEntity socre  = new EvaluateScoreEntity();
        CriteriaFilter filter = new CriteriaFilter();
        filter.setEvalId(evalId);
        List<CriteriaEntity> criteriaEntities = criteriaService.selectListCriteria(filter);//考评规则
        if(ObjectUtils.isBlank(criteriaEntities))
        {
            socre.setGrades("-");
            socre.setFianlScore(0);
            return socre;
        }
        //判断类型 是否为累计类；
        CriteriaEntity entity = criteriaEntities.get(0);

        socre= CaluateScore(entity, calculates,grade);
        return socre;
    }

    /**
     *
    按照规则去计算的得分
     */
    public EvaluateScoreEntity CaluateScore(CriteriaEntity cra, int calculates,String grades)
    {
        EvaluateScoreEntity entity = new EvaluateScoreEntity();
        double sum =0;
        if(1==cra.getCriteriaType())
        {
            sum=calculates*cra.getCriteriaValue();
            entity.setFianlScore(sum);
        }else if(2==cra.getCriteriaType())
        {
            if(StringUtils.isBlank(grades)){
                int rank = calculates;
                if (rank > 0 && rank <= cra.getGoodStandard()) {
                    sum = cra.getGoodValue();
                    entity.setGrades("好");
                }
                if (rank > (cra.getGoodStandard()) && rank <=cra.getGeneralStandard()) {
                    sum =  cra.getGeneralValue();
                    entity.setGrades("中");
                }
                if (rank > (cra.getGeneralStandard()) && rank < (cra.getBadStandard()+1)) {
                    sum =  cra.getBadValue();
                    entity.setGrades("差");
                }
                entity.setFianlScore(sum);
            }else {

                switch (grades){
                    case "好":
                        entity.setFianlScore(cra.getGoodValue());
                        break;
                    case "中":
                        entity.setFianlScore(cra.getGeneralValue());
                        break;
                    case "差":
                        entity.setFianlScore(cra.getBadValue());
                        break;
                }

            }


        }



        return  entity;

    }

    //总得分
    @Override
    public double leaderTotalScore(Long leadId) {
        double sum =0;
        List<EvaluateScoreEntity> lists = evaScoreMageMapper.queryEvainfo(null, leadId + "");//考评类型
        ArrayList<EvaluateScoreEntity> collect = lists.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(EvaluateScoreEntity::getEvaId))), ArrayList::new));
        for (EvaluateScoreEntity item : collect) {

            double score = singerItemScore(item.getEvaId(), leadId);//一整个大类的分数统计
            sum=sum+score;
        }

        return sum;
    }

    // 单个考评的得分
    @Override
    public double singerItemScore(Long evalId, Long leaderId) {
        double sumScore =0;
        List<EvaluateScoreEntity> lists = evaScoreMageMapper.queryEvainfo(evalId, leaderId + "");//考评类型

            CriteriaFilter filter = new CriteriaFilter();
            filter.setEvalId(evalId);
            List<CriteriaEntity> criteriaEntities = criteriaService.selectListCriteria(filter);//考评规则
            if(ObjectUtils.isBlank(criteriaEntities))
            {
                return 0;
            }
            //判断类型 是否为累计类；
            CriteriaEntity entity = criteriaEntities.get(0);
        for (EvaluateScoreEntity list : lists) { //获取考评规则
            double score = CaluateScore(entity, list.getCalculates(),list.getGrades()).getFianlScore();//计算分值
                list.setFianlScore(score);
                sumScore=score+sumScore;//累加
        }
        return sumScore;


    }

    /**
     * 每个考评类型的得分
     * @param LeadId
     * @return
     */
    @Override
    public List<EvaluateScoreEntity> eachItemScore(Long LeadId) {
        List<EvaluateIndexFilter> evaluateLists = evaScoreMageMapper.queryEvaMag(new EvaluateIndexFilter());
        List<EvaluateScoreEntity> scoreList  = new ArrayList<>();
        for (EvaluateIndexFilter item : evaluateLists) {
            EvaluateScoreEntity score = new EvaluateScoreEntity();
            score.setEvaId(item.getId());
            score.setCriteriaType(item.getCriteriaType());
            double itemScore = singerItemScore(item.getId(), LeadId);
            score.setFianlScore(DataUtil.formatNumberDouble(2,itemScore));
            score.setAssessment(item.getIndexName());
            score.setConnects(item.getConnects());
            scoreList.add(score);
        }
        return scoreList;
    }


}
